﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Licenta_Stomatologie.Helpers
{
    class PDFGenerator
    {
        private iTextSharp.text.Document document;
        private iTextSharp.text.pdf.PdfWriter writer;
        private String path;
        private String fileName;
        private ProjectDBEntities db = new ProjectDBEntities();

        public PDFGenerator()
        {
            
        }

        private bool choosePath()
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                System.Windows.Forms.MessageBox.Show(ErrorMessages.ERR_CHOOSE_FOLDER);
                return false;
            }

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                path = dialog.SelectedPath;
                fileName = "File.pdf";
            }
            return true;
        }

        public void generateReceipt(String[] data)
        {
            if (!choosePath())
            {
                return;
            }

            iTextSharp.text.Paragraph header = prepareHeader();
            if (header == null)
            {
                System.Windows.Forms.MessageBox.Show(ErrorMessages.ERR_NO_COMPANY);
                return;
            }

            String medicName = data[0];
            int pacientId = int.Parse(data[1]);
            String disease = data[2];
            String treatment = data[3];

            iTextSharp.text.Paragraph rec = new iTextSharp.text.Paragraph("Reteta : ");
            rec.SpacingBefore = 10;
            rec.SpacingAfter = 10;
            rec.Alignment = 1;

            iTextSharp.text.Paragraph medic = new iTextSharp.text.Paragraph("Medic : " + medicName);
            medic.SpacingBefore = 10;
            medic.SpacingAfter = 10;
            medic.Alignment = 1;

            iTextSharp.text.Paragraph pd = new iTextSharp.text.Paragraph("Detalii pacient : ");
            iTextSharp.text.Paragraph details = preparePacientDetails(pacientId);
            pd.SpacingBefore = 10;
            pd.SpacingAfter = 10;
            pd.Alignment = 1;

            iTextSharp.text.Paragraph boala = new iTextSharp.text.Paragraph("Boala : " + disease);
            boala.SpacingBefore = 10;
            boala.SpacingAfter = 10;
            boala.Alignment = 1;

            iTextSharp.text.Paragraph tratament = new iTextSharp.text.Paragraph("Tratament : " + treatment);
            tratament.SpacingBefore = 10;
            tratament.SpacingAfter = 10;
            tratament.Alignment = 1;

            document = new iTextSharp.text.Document();
            fileName = getFileName(pacientId);
            writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(path + "\\" + fileName, System.IO.FileMode.Create));
            document.Open();

            document.Add(rec);
            document.Add(header);
            document.Add(medic);
            document.Add(pd);
            document.Add(details);
            document.Add(boala);
            document.Add(tratament);

            document.Close();

            System.Windows.Forms.MessageBox.Show("Fisier salvat cu succes");
            return;
        }

        public void generatePDF()
        {
            if (!choosePath())
            {
                return;
            }

            iTextSharp.text.Paragraph header = prepareHeader();
            if (header == null)
            {
                System.Windows.Forms.MessageBox.Show(ErrorMessages.ERR_NO_COMPANY);
                return;
            }
            document = new iTextSharp.text.Document();
            writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(path + "\\" + fileName, System.IO.FileMode.Create));
            document.Open();

            document.Add(header);

            document.Close();
        }

        public bool generatePacientFile(int pacientId)
        {
            if (!choosePath())
            {
                return false;
            }

            iTextSharp.text.Paragraph header = prepareHeader();
            if (header == null)
            {
                System.Windows.Forms.MessageBox.Show(ErrorMessages.ERR_NO_COMPANY);
                return false;
            }
            iTextSharp.text.Paragraph pd = new iTextSharp.text.Paragraph("Detalii pacient : ");
            iTextSharp.text.Paragraph details = preparePacientDetails(pacientId);
            pd.SpacingBefore = 10;
            pd.SpacingAfter = 10;
            pd.Alignment = 1;

            iTextSharp.text.Paragraph diseases = new iTextSharp.text.Paragraph("Boli netratate : ");
            diseases.SpacingBefore = 10;
            diseases.SpacingAfter = 10;
            diseases.Alignment = 1;

            ProjectDBEntities db = new ProjectDBEntities();

            IEnumerable<Afectiuni> afectiuni = db.Afectiunis.Where(a => a.pacientId == pacientId && a.status == 0);
            String[] boli = new String[] {"Abces dentar","Abrazie","Anodontie","Eroziune"};
            foreach (Afectiuni aff in afectiuni)
            {
                diseases.Add(new iTextSharp.text.Paragraph("Dintele : " + aff.teeth + ". Afectiune : " + boli[(int)aff.disease] + ". Tratament : " + aff.treatment));
            }

            document = new iTextSharp.text.Document();
            fileName = getFileName(pacientId);
            writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(path + "\\" + fileName, System.IO.FileMode.Create));
            document.Open();

            document.Add(header);
            document.Add(pd);
            document.Add(details);
            document.Add(diseases);

            document.Close();

            System.Windows.Forms.MessageBox.Show("Fisier salvat cu succes");
            return true;
        }

        private iTextSharp.text.Paragraph prepareHeader()
        {
            iTextSharp.text.Paragraph header = new iTextSharp.text.Paragraph();
            if (db.Companies.Count() == 0)
            {
                return null;
            }

            Company comp = db.Companies.First(c => c.id > 0);

            iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(2);

            iTextSharp.text.pdf.PdfPCell topLeftCell = new iTextSharp.text.pdf.PdfPCell();
            topLeftCell.BorderWidthRight = 0f;
            topLeftCell.BorderWidthBottom = 0f;
            topLeftCell.Phrase = new iTextSharp.text.Phrase("Judetul");

            iTextSharp.text.pdf.PdfPCell topRightCell = new iTextSharp.text.pdf.PdfPCell();
            topRightCell.BorderWidthLeft = 0f;
            topRightCell.BorderWidthBottom = 0f;
            topRightCell.Phrase = new iTextSharp.text.Phrase(comp.judet);

            iTextSharp.text.pdf.PdfPCell middleCell = new iTextSharp.text.pdf.PdfPCell();
            middleCell.BorderWidth = 0f;

            iTextSharp.text.pdf.PdfPCell bottomLeftCell = new iTextSharp.text.pdf.PdfPCell();
            bottomLeftCell.BorderWidthRight = 0f;
            bottomLeftCell.BorderWidthTop = 0f;
            bottomLeftCell.Phrase = new iTextSharp.text.Phrase("Unitatea sanitara");

            iTextSharp.text.pdf.PdfPCell bottomRightCell = new iTextSharp.text.pdf.PdfPCell();
            bottomRightCell.BorderWidthLeft = 0f;
            bottomRightCell.BorderWidthTop = 0f;
            bottomRightCell.Phrase = new iTextSharp.text.Phrase(comp.unitateSanitara);

            table.AddCell(topLeftCell);
            table.AddCell(topRightCell);

            middleCell.Phrase = new iTextSharp.text.Phrase("Localitatea");
            table.AddCell(middleCell);
            middleCell.Phrase = new iTextSharp.text.Phrase(comp.localitate);
            table.AddCell(middleCell);

            middleCell.Phrase = new iTextSharp.text.Phrase("Adresa");
            table.AddCell(middleCell);
            middleCell.Phrase = new iTextSharp.text.Phrase(comp.adresa);
            table.AddCell(middleCell);

            table.AddCell(bottomLeftCell);
            table.AddCell(bottomRightCell);

            header.Add(table);

            return header;
        }

        private iTextSharp.text.Paragraph preparePacientDetails(int pacientId)
        {
            ProjectDBEntities db = new ProjectDBEntities();

            Pacient pacient = db.Pacients.First(p => p.id == pacientId);

            iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(2);

            iTextSharp.text.pdf.PdfPCell topLeftCell = new iTextSharp.text.pdf.PdfPCell();
            topLeftCell.BorderWidthRight = 0f;
            topLeftCell.BorderWidthBottom = 0f;

            iTextSharp.text.pdf.PdfPCell topRightCell = new iTextSharp.text.pdf.PdfPCell();
            topRightCell.BorderWidthLeft = 0f;
            topRightCell.BorderWidthBottom = 0f;

            iTextSharp.text.pdf.PdfPCell middleCell = new iTextSharp.text.pdf.PdfPCell();
            middleCell.BorderWidth = 0f;

            iTextSharp.text.pdf.PdfPCell bottomLeftCell = new iTextSharp.text.pdf.PdfPCell();
            bottomLeftCell.BorderWidthRight = 0f;
            bottomLeftCell.BorderWidthTop = 0f;

            iTextSharp.text.pdf.PdfPCell bottomRightCell = new iTextSharp.text.pdf.PdfPCell();
            bottomRightCell.BorderWidthLeft = 0f;
            bottomRightCell.BorderWidthTop = 0f;

            topLeftCell.Phrase = new iTextSharp.text.Phrase("Nume");
            topRightCell.Phrase = new iTextSharp.text.Phrase(pacient.nume);

            table.AddCell(topLeftCell);
            table.AddCell(topRightCell);

            middleCell.Phrase = new iTextSharp.text.Phrase("Prenume");
            table.AddCell(middleCell);
            middleCell.Phrase = new iTextSharp.text.Phrase(pacient.prenume);
            table.AddCell(middleCell);

            middleCell.Phrase = new iTextSharp.text.Phrase("Sex");
            table.AddCell(middleCell);
            middleCell.Phrase = new iTextSharp.text.Phrase(pacient.sex);
            table.AddCell(middleCell);

            middleCell.Phrase = new iTextSharp.text.Phrase("Varsta");
            table.AddCell(middleCell);
            middleCell.Phrase = new iTextSharp.text.Phrase(DateTime.Now.Year - pacient.dataNasterii.Value.Year);
            table.AddCell(middleCell);

            middleCell.Phrase = new iTextSharp.text.Phrase("Localitatea");
            table.AddCell(middleCell);
            middleCell.Phrase = new iTextSharp.text.Phrase(pacient.localitate);
            table.AddCell(middleCell);

            bottomLeftCell.Phrase = new iTextSharp.text.Phrase("Adresa");
            bottomRightCell.Phrase = new iTextSharp.text.Phrase(pacient.adresa);

            table.AddCell(bottomLeftCell);
            table.AddCell(bottomRightCell);

            iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph();
            paragraph.Add(table);

            return paragraph;
        }

        private String getFileName(int pacientId)
        {
            ProjectDBEntities db = new ProjectDBEntities();

            Pacient pac = db.Pacients.First(p => p.id == pacientId);

            return pac.nume + pac.prenume + ".pdf";
        }
    }
}
